package com.ccs.demo.service;

import com.ccs.demo.dao.BookDao;
import com.ccs.demo.enums.BookStatus;
import com.ccs.demo.mapper.BookMapper;
import com.ccs.demo.model.BookInfo;
import com.ccs.demo.model.PageRequest;
import com.ccs.demo.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;


    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getListByPage(PageRequest pageRequest) {
        //获取记录总数
        Integer count=bookMapper.count();
        //获取当前页的信息
        List<BookInfo> bookInfos= bookMapper.queryListByLimit(pageRequest);
        for(BookInfo bookInfo: bookInfos){
           bookInfo.setStatusCn(BookStatus.getStatusByCode(bookInfo.getStatus()).getDesc());
        }

        return new PageResult<>(count,bookInfos);
    }

    public List<BookInfo> queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }


    public  Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer DeleteBook(Integer bookId) {
        return bookMapper.DeleteBook(bookId);
    }

    public Integer batchDeleteBook(List<Integer> ids) {
        return bookMapper.batchDeleteBook(ids);
    }
}

//    @Autowired
//    private BookDao bookDao;

//    public List<BookInfo> getList(){
//        BookDao bookDao=new BookDao();
//        List<BookInfo> bookInfos=bookDao.mockData();
//        for(BookInfo bookInfo: bookInfos){
//            if(bookInfo.getStatus() == 2){
//                bookInfo.setStatusCn("不可借阅");
//            }else{
//                bookInfo.setStatusCn("可借阅");
//            }
//        }
//        return bookInfos;
//    }